{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit ConfigurationForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, NavigationManager;

type
  TfrmNavigationConfig = class(TForm)
    btClose: TButton;
    gbNavSettings: TGroupBox;
    Label1: TLabel;
    ecApplications: TComboBox;
    btDetect: TButton;
    gbLaunchSettings: TGroupBox;
    cbAutoLaunch: TCheckBox;
    Label2: TLabel;
    edPath: TEdit;
    btBrowse: TButton;
    Label4: TLabel;
    edParams: TEdit;
    gbEmbedSettings: TGroupBox;
    cbAutoEmbed: TCheckBox;
    Label3: TLabel;
    edWinName: TEdit;
    Label5: TLabel;
    edWinClass: TEdit;
    cbWinLock: TCheckBox;
    procedure btDetectClick(Sender: TObject);
    procedure ecApplicationsChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btCloseClick(Sender: TObject);
    procedure btBrowseClick(Sender: TObject);
    procedure cbSettingsClick(Sender: TObject);
  private
    { Private declarations }
    FWorking: Boolean;
    FNavigation: TAHMNavigationManager;
    function GetCurrent: TAHMNavigationApplication;
    procedure PopulateApplications;
    procedure ShowApplicationSettings;
    procedure UpdateSettingsControls;
    procedure UpdateApplicationSettings;
  protected
    { Protected declarations }
    property CurrentApplication: TAHMNavigationApplication read GetCurrent;
    property NavigationManager: TAHMNavigationManager read FNavigation write FNavigation;
  public
    { Public declarations }
    class function Configure(Navigation: TAHMNavigationManager): Boolean;
  end;


implementation

{$R *.dfm}

uses Utilities;

class function TfrmNavigationConfig.Configure(Navigation: TAHMNavigationManager): Boolean;
begin
  with TfrmNavigationConfig.Create(nil) do
  try
    NavigationManager := Navigation;
    Result := (ShowModal = mrOK);
  finally
    Free;
  end;
end;

procedure TfrmNavigationConfig.FormShow(Sender: TObject);
begin
  // Populate available applications and display current configuration details
  PopulateApplications;
end;

procedure TfrmNavigationConfig.ecApplicationsChange(Sender: TObject);
begin
  // Display details for selected application
  if not FWorking then ShowApplicationSettings;
end;

procedure TfrmNavigationConfig.cbSettingsClick(Sender: TObject);
begin
  // Configure launch/embed settings controls
  UpdateSettingsControls;
end;

procedure TfrmNavigationConfig.btDetectClick(Sender: TObject);
begin
  // Reset current application and repopulate recommendation
  FNavigation.Application.Clear;
  PopulateApplications;
end;

procedure TfrmNavigationConfig.btBrowseClick(Sender: TObject);
var
  Filename: String;
begin
  // Browse for a custom application
  Filename := edPath.Text;
  if ChooseFile('Select application', Filename, False) then
    edPath.Text := Filename;
end;

procedure TfrmNavigationConfig.btCloseClick(Sender: TObject);
begin
  // Update modified application settings
  UpdateApplicationSettings;
end;

function TfrmNavigationConfig.GetCurrent: TAHMNavigationApplication;
begin
  with ecApplications do
    if (ItemIndex >= 0) and (ItemIndex < Items.Count) then
      Result := TAHMNavigationApplication(Items.Objects[ItemIndex])
    else
      Result := nil;
end;

procedure TfrmNavigationConfig.PopulateApplications;
var
  i: Integer;
  Work, Current: TAHMNavigationApplication;
begin
  FWorking := True;
  try
    ecApplications.Items.Clear;

    // Get recommended application
    Current := FNavigation.Application;
    FNavigation.Applications.AutoDetect(Current);

    // Populate list of detected applications
    for i := Pred(FNavigation.Applications.Count) downto 0 do
    begin
      Work := FNavigation.Applications[i];
      ecApplications.Items.AddObject(Work.Name, Work);

      // Is this our configured application?
      if Work.Name = Current.Name then
      begin
        // Select this in combo and copy any custom settings
        ecApplications.ItemIndex := Pred(ecApplications.Items.Count);
        Work.Assign(Current);
      end;
    end;

    ShowApplicationSettings;
  finally
    FWorking := False;
  end;
end;

procedure TfrmNavigationConfig.ShowApplicationSettings;
var
  Current: TAHMNavigationApplication;
begin
  Current := GetCurrent;

  if Assigned(Current) then edPath.Text := Current.Path else edPath.Text := '';
  if Assigned(Current) then edParams.Text := Current.Parameters else edParams.Text := '';
  if Assigned(Current) then edWinName.Text := Current.WindowName else edWinName.Text := '';
  if Assigned(Current) then edWinClass.Text := Current.WindowClass else edWinClass.Text := '';
  if Assigned(Current) then cbWinLock.Checked := Current.WindowLock else cbWinLock.Checked := False;

  cbAutoLaunch.Enabled := Assigned(Current) and not Current.ReadOnly;
  cbAutoEmbed.Enabled := Assigned(Current) and not Current.ReadOnly;
  cbAutoLaunch.Checked := (edPath.Text <> '');
  cbAutoEmbed.Checked := (edWinName.Text <> '') or (edWinClass.Text <> '');

  // Ensure settings controls are in sync with this application
  UpdateSettingsControls;
end;

procedure TfrmNavigationConfig.UpdateSettingsControls;
begin
  edPath.Enabled := cbAutoLaunch.Checked and cbAutoLaunch.Enabled;
  edParams.Enabled := cbAutoLaunch.Checked and cbAutoLaunch.Enabled;
  btBrowse.Enabled := cbAutoLaunch.Checked and cbAutoLaunch.Enabled;

  edWinName.Enabled := cbAutoEmbed.Checked and cbAutoEmbed.Enabled;
  edWinClass.Enabled := cbAutoEmbed.Checked and cbAutoEmbed.Enabled;
  cbWinLock.Enabled := cbAutoEmbed.Checked and cbAutoEmbed.Enabled;
end;

procedure TfrmNavigationConfig.UpdateApplicationSettings;
begin
  // First copy internal details of selected application profile
  FNavigation.Application.Assign(GetCurrent);

  // Now populate any custom details that may have been entered
  FNavigation.Application.Path := edPath.Text;
  FNavigation.Application.Parameters := edParams.Text;
  FNavigation.Application.WindowName := edWinName.Text;
  FNavigation.Application.WindowClass := edWinClass.Text;
  FNavigation.Application.WindowLock := cbWinLock.Checked;
end;





end.
